package com.smartwebx.ha.web.gis.module.screen;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.rainwater.feature.FeatureRainfallManager;
import com.smartwebx.ha.biz.rainwater.real.LayerRainfallManager;
import com.smartwebx.ha.dal.annot.JsonModule;
import com.smartwebx.ha.dal.annot.JsonModules;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.DateValueTotal;
import com.smartwebx.ha.dal.usebean.FeatureRainSlideDay;
import com.smartwebx.ha.dal.usebean.RainSlideDay;
import com.smartwebx.ha.dal.usebean.RainSlideHour;
import com.smartwebx.ha.dal.usebean.SuperStatGroup;
import com.smartwebx.ha.dal.usebean.view.ViewRainSlideDay;
import com.smartwebx.ha.dal.usebean.view.ViewRainSlideHour;
import com.smartwebx.ha.dal.util.MyFunctions;
import com.smartwebx.ha.web.util.json.module.DateModule;
import com.smartwebx.ha.web.util.json.module.DoubleModule;

public class LayerRainfallScreen extends BasicScreen {
	@Autowired
	private LayerRainfallManager layerRainfallManager;
	@Autowired
	private FeatureRainfallManager featureRainfallManager;
	@Autowired
	private HttpServletRequest request;

	/**
	 * 日面雨量过程(zx)
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws ParseException
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
			@JsonModule(clazz = DoubleModule.class, value = "1") })
	public List<DateValueTotal> getDayLayerProcess(String stcd, Date bTm,
			Date eTm) throws IOException {
		List<DateValue> dateValueList = layerRainfallManager
				.getDayLayerRainfall2(stcd, bTm, eTm);

		return Lists.transform(dateValueList,
				MyFunctions.transformDateValueTotal());
	}

	@JsonModule(clazz = DoubleModule.class, value = "1", isNumber = true)
	public Object getDayLayerProcessByGisJs(String stcd, Date bTm, Date eTm)
			throws IOException {
		List<DateValue> dateValueList = layerRainfallManager
				.getDayLayerRainfall2(stcd, bTm, eTm);

		List<DateValueTotal> dataValueTotalList = Lists.transform(
				dateValueList, MyFunctions.transformDateValueTotal());

		List<Double> vals = Lists.newArrayList();
		List<Double> totals = Lists.newArrayList();
		for (DateValueTotal valTotal : dataValueTotalList) {
			vals.add(valTotal.getVal());
			totals.add(valTotal.getTotalVal());
		}

		Map<String, List<Double>> result = ImmutableMap.of("rainVals", vals,
				"rainTotVals", totals);
		return result;

	}

	/**
	 * 各分区面雨量
	 * 
	 * @param bTm
	 * @param eTm
	 * @param type
	 * @return
	 * @throws ParseException
	 */
	@JsonModule(clazz = DoubleModule.class, value = "1")
	public List<SuperStatGroup> getLayerRainfall(Date bTm, Date eTm) {
		// List<SuperStatGroup> superStatGroupList = layerRainfallManager
		// .getLayerRainfall(bTm, eTm, getAreaId());
		//
		// List<SuperStatGroup> superStatGroups = new
		// ArrayList<SuperStatGroup>();
		// for (SuperStatGroup superStatGroup : superStatGroupList) {
		// if (!superStatGroup.getAtype().equals("水资源分区")) {
		// superStatGroups.add(superStatGroup);
		// }
		// }
		//
		// return superStatGroups;
		return null;
	}

	/**
	 * 小时面雨量过程(zx)
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws ParseException
	 */
	@JsonModules({
			@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm"),
			@JsonModule(clazz = DoubleModule.class, value = "1") })
	public List<DateValueTotal> getHourLayerProcess(String stcd, Date bTm,
			Date eTm) {
		List<DateValue> dateValueList = layerRainfallManager
				.getHourLayerRainfall2(stcd, bTm, eTm);
		return Lists.transform(dateValueList,
				MyFunctions.transformDateValueTotal());
	}

	@JsonModule(clazz = DoubleModule.class, value = "1", isNumber = true)
	public Object getHourLayerProcessByGisJs(String stcd, Date bTm, Date eTm) {
		List<DateValue> dateValueList = layerRainfallManager
				.getHourLayerRainfall2(stcd, bTm, eTm);
		List<DateValueTotal> dateValueTotalList = Lists.transform(
				dateValueList, MyFunctions.transformDateValueTotal());
		List<Double> vals = Lists.newArrayList();
		List<Double> totals = Lists.newArrayList();
		for (DateValueTotal valTotal : dateValueTotalList) {
			vals.add(valTotal.getVal());
			totals.add(valTotal.getTotalVal());
		}

		Map<String, List<Double>> result = ImmutableMap.of("rainVals", vals,
				"rainTotVals", totals);
		return result;
	}

	/**
	 * 面日降雨滑动分析
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws ParseException
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
			@JsonModule(clazz = DoubleModule.class, value = "1") })
	public ViewRainSlideDay getRainLayerSlideDay(String stcd, Date bTm, Date eTm)
			throws IllegalAccessException, InvocationTargetException {
		List<DateValue> dateValueList = layerRainfallManager
				.getDayLayerRainfall(stcd, bTm, eTm);
		RainSlideDay rainSlide = layerRainfallManager.getRainSlideDay(stcd,
				dateValueList);
		FeatureRainSlideDay featureRainSlideDay = featureRainfallManager
				.getRainSlideDay(stcd);
		ViewRainSlideDay viewRainSlideDay = new ViewRainSlideDay(
				featureRainSlideDay.getOneDay(),
				featureRainSlideDay.getThreeDay(),
				featureRainSlideDay.getSevenDay(),
				featureRainSlideDay.getFifteenDay(),
				featureRainSlideDay.getThirtyDay());
		BeanUtils.copyProperties(viewRainSlideDay, rainSlide);
		return viewRainSlideDay;

	}

	/**
	 * 面小时过程滑动分析
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws ParseException
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
			@JsonModule(clazz = DoubleModule.class, value = "1") })
	public ViewRainSlideHour getRainLayerSlideHour(String stcd, Date bTm,
			Date eTm) throws Exception {
		List<DateValue> dateValueList = layerRainfallManager
				.getHourLayerRainfall(stcd, bTm, eTm);
		RainSlideHour rainSlideHour = layerRainfallManager.getRainSlideHour(
				stcd, dateValueList);
		FeatureRainSlideDay featureRainSlideDay = featureRainfallManager
				.getRainSlideDay(stcd);
		ViewRainSlideHour viewRainSlideHour = new ViewRainSlideHour(
				featureRainSlideDay.getOneHour(),
				featureRainSlideDay.getThreeHour(),
				featureRainSlideDay.getSixHour(),
				featureRainSlideDay.getTwelveHour(),
				featureRainSlideDay.getTwentyFourHour());
		BeanUtils.copyProperties(viewRainSlideHour, rainSlideHour);
		return viewRainSlideHour;

	}

}
